package com.gupao.pattern.proxy.dbroute.proxy;

import com.gupao.pattern.proxy.dbroute.IOrderService;
import com.gupao.pattern.proxy.dbroute.Order;
import com.gupao.pattern.proxy.dbroute.db.DynamicDataSourceEntity;

import java.text.SimpleDateFormat;
import java.util.Date;

public class OrderServiceStaticProxy implements IOrderService {

    private SimpleDateFormat format=new SimpleDateFormat("yyyy");

    private IOrderService orderService;

    public OrderServiceStaticProxy(IOrderService orderService){
        this.orderService=orderService;
    }

    public int save(Order order) {
        Long time=order.getCreateTime();
        Integer dbRouter=Integer.valueOf(format.format(new Date(time)));
        System.out.println("静态代理类自动分配到【DB_"+dbRouter+"】数据源处理数据");
        DynamicDataSourceEntity.set(dbRouter);

        this.orderService.save(order);

        DynamicDataSourceEntity.restore();
        return 0;
    }
}
